package com.aaa.eat.service.Impl;

import com.aaa.eat.mapper.EmpMapper;
import com.aaa.eat.mapper.LoginMapper;
import com.aaa.eat.mapper.PermissionMapper;
import com.aaa.eat.pojo.domain.Emp;
import com.aaa.eat.pojo.domain.Login;
import com.aaa.eat.pojo.domain.Permission;
import com.aaa.eat.pojo.domain.User;
import com.aaa.eat.result.AjaxResult;
import com.aaa.eat.service.EmpService;
import com.aaa.eat.syscount.LoginCount;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * Author:张海金
 * Description:T0D0
 * DateTime:2022/1/20 18:17
 **/
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult login(String username, String password) {

        Login login = loginMapper.isLogin(username, password);

        // 1 验证账号密码是否正确
        if(login == null){
            return AjaxResult.fail(LoginCount.EMP_MSG_CUOWU);
        }
        if(login.getState().equals(LoginCount.EMP_STATE_DONGJIE)){
            return AjaxResult.fail(LoginCount.EMP_MSG_DONGJIE);
        }
        if(login.getState().equals(LoginCount.EMP_STATE_ZHUXIAO)){
            return AjaxResult.fail(LoginCount.EMP_MSG_ZHUXIAO);
        }

        // 2 获取该账号对应的用户信息，并存储到session中
        Integer eid = login.getEid();

        Emp emp = empMapper.getEmpByID(eid);

        session.setAttribute(LoginCount.EMP_IN_SESSION,emp);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInfo() {
        //获取session中存储的登录信息，并返回
        Emp emp = (Emp) session.getAttribute(LoginCount.EMP_IN_SESSION);

        HashMap<Object, Object> data = new HashMap<>();
        data.put("currentEmp",emp);

        Integer state = emp.getRole().getState();

        if( emp.getRole().getState() == 0 ){
            if( emp.getRole().getId() == 1 ){
                //获取超级管理员权限
                List<Permission> supperAdminPermission = permissionMapper.getSupperAdminPermission();
                data.put("currentPermission",supperAdminPermission);
            }else {
                //获取非超级管理员权限
            }
        }

        return AjaxResult.success(data);
    }

    @Override
    public AjaxResult denglu(@Param("zhanghao") String zhanghao,@Param("mima") String mima) {
        User user = empMapper.denglu(zhanghao,mima);
        if( user == null ){
            return AjaxResult.fail("对不起，账号或者密码错误");
        }else if ( user.getState() == 1 ){
            return AjaxResult.fail("对不起，你的账号已被冻结");
        }
        return AjaxResult.success(user);
    }

    @Override
    public AjaxResult getName(Integer id) {
        User userMassage = empMapper.getName(id);
        return AjaxResult.success(userMassage);
    }


}
